package com.willian.andrappfw.ui;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.willian.andrappfw.R;


public class TimeOutDialog extends BaseDialog {

    /*** 显示的图片*/
    private ImageView imageIv ;
    /*** 显示的消息*/
    private TextView messageTv ;
    /*** retry 按钮*/
    TextView retry;

    /*** 都是内容数据*/
    private final int layoutId;
    private final String message;
    private final int imageResId;

    public TimeOutDialog(@NonNull Context context, int layoutId, String message, int imageResId) {
        super(context);

        this.layoutId = layoutId;
        this.message = message;
        this.imageResId = imageResId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //绑定布局文件
        setContentView(layoutId);

        //初始化界面
        initView();
        //刷新显示数据以及界面
        refreshView();
    }

    /**
     * 初始化界面控件
     */
    protected void initView(){
        //获得点击按钮对象
        retry = findViewById(R.id.textView_reTry);
        //点击任意位置，关闭dialog
        retry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimeOutDialog.this.dismiss();
            }
        });

        messageTv = findViewById(R.id.textView_tip);
        imageIv = findViewById(R.id.imageView_tip);
    }

    /**
     * 初始化界面控件的显示数据
     */
    protected void refreshView() {
        //如果用户自定了title和message
        if (!TextUtils.isEmpty(message)) {
            messageTv.setText(message);
            messageTv.setVisibility(View.VISIBLE);
        }else {
            messageTv.setVisibility(View.GONE);
        }

        //处理上面的图标
        if (imageResId != -1){
            imageIv.setImageResource(imageResId);
            imageIv.setVisibility(View.VISIBLE);
        }else {
            imageIv.setVisibility(View.GONE);
        }
    }
}
